/**
 * Created by hjqi on 2016/8/30.
 */
module.controller('ClusterDetailController',
    function ($scope, $rootScope,$state, $stateParams, $cookies,clusterService) {

    // 服务模板
    var user = $cookies.get("username");
    $scope.username = user;
    var _clusterId = $stateParams.clusterId;
    if($stateParams.cluster === undefined || $stateParams.cluster === null ){
        loadCluster(_clusterId);
    }else{
        if($stateParams.cluster.nodes === undefined)
            $scope.nodes = [];
        else
            //$scope.nodes = convertNodesObj2Array($stateParams.cluster.nodes,0);
            reqNodeInfoFromK8s($stateParams.cluster.nodes);
    }

    $scope.currentPageServices = [];
    $scope.currentPage = 1;
    $scope.numPerPage = 10;
    $scope.$watchCollection("nodes + currentPage", function() {
        //var currentClusters = [];
        if ($scope.nodes) {
           /* var begin = (($scope.currentPage - 1) * $scope.numPerPage)
                , end = begin + $scope.numPerPage;
            //var currentNodes = convertNodesObj2Array($scope.nodes,begin,end); //$scope.nodes.slice(begin, end);
            var currentNodes = $scope.nodes.slice(begin, end);
            $scope.currentPageServices = currentNodes;*/
            //fix other node info
            //reqNodeInfoFromK8s(currentNodes);

            fillCurData();
        }

    },true);

    $scope.$watchCollection('nodes', function (newvalue,oldvalue) {
        if(newvalue !== undefined)
            $scope.nodes = newvalue;

        if($scope.refreshEvt === true){
            $scope.refreshEvt = false;
            fillCurData();
        }
    });

    $scope.addCluster = function(){
        $state.go("cp.addclusternode",{clusterId:_clusterId});
    }

    $scope.refreshCluster = function(){
        $scope.refreshEvt = true;
        loadCluster(_clusterId);
    }

    $scope.deleteClusterNode = function(data){
        clusterService.deleteClusterNode(data.clusterId,data.nodeId).then(function(ret){
            if(ret.data.ret ===true){
                var _idx = $scope.currentPageServices.indexOf(data);
                $scope.currentPageServices.splice(_idx,1);
                alertify.success('delete node success');
            }else{
                alertify.error(ret.data.message);
            }
        }).catch(function(err){
            alertify.alert('delete cluster nodes err', err.data);
        });
    }

     function fillCurData(){
         var begin = (($scope.currentPage - 1) * $scope.numPerPage)
             , end = begin + $scope.numPerPage;
         //var currentNodes = convertNodesObj2Array($scope.nodes,begin,end); //$scope.nodes.slice(begin, end);
         var currentNodes = $scope.nodes.slice(begin, end);
         $scope.currentPageServices = currentNodes;
     }

    function loadCluster(clusterId){
        clusterService.getClustersInfo(clusterId).then(function(rets){
            if(rets.data[0].nodes !== undefined){
                //$scope.nodes = rets.data[0].nodes;
                reqNodeInfoFromK8s(rets.data[0].nodes);
            }
            else
                $scope.nodes = {};
        }).catch(function(err){
            alertify.alert('remote cluster severs is not ready');
        });

        //req k8s nodes interface


    }

    function convertNodesObj2Array(nodes,begin,end){
        var _ret = [];
        var _idx = 0;
        for(var id in nodes){
            if(end === undefined){
                _ret.push(getNodeInfo(id,nodes[id]));
            }else{
                if( _idx >= end ){
                    break;
                }
                else if( ( _idx >= begin) && _idx < end){
                    _ret.push(getNodeInfo(id,nodes[id]));
                }
            }
            _idx++;
        }
        return _ret;
    }

    function getNodeInfo(nodeId,node){
        var _ret = {};
        //_ret.host = node.spec.externalID;
        _ret.host = node.host;
        _ret.status = (node.status === undefined? 'disconnected':node.status);
        _ret.nodeId = nodeId;
        _ret.clusterId = _clusterId;
        _ret.cpu = (node.cpu === undefined? '':node.cpu);
        _ret.memory = (node.memory === undefined? '':node.memory);

        return _ret;
    }

    function reqNodeInfoFromK8s(nodes){

        clusterService.getNodeInfoByPage(_clusterId,nodes).then(function(ret){
            if(ret.data.ret === true){
                //$scope.currentPageServices = ret.data.nodes;
                $scope.nodes =convertNodesObj2Array(ret.data.nodes,0);
            }else{
                $scope.nodes = convertNodesObj2Array(nodes,0);
                //$scope.currentPageServices = nodes;
            }
        }).catch(function(err){
            //alertify.error('get cluster nodes from k8s err', err);
            alertify.error('cluster nodes is not ready');
            //$scope.currentPageServices = nodes;
            $scope.nodes = convertNodesObj2Array(nodes,0);
        });

    }
});
